"use strict";

//模拟加载新闻
var newsColumn_1 = [];
var newsColumn_2 = [];
var newsColumn_3 = [];
var informationIndustry_1 = [];
var informationIndustry_2 = [];
var informationIndustry_3 = [];
var practice_1 = [];
var practice_2 = [];
var practice_3 = [];
var cooperation_1 = [];
var cooperation_2 = [];
var cooperation_3 = [];
var economics_1 = [];
var economics_2 = [];
var economics_3 = [];
// 专家轮播数据
var expertArrList = [];
var realTinfoArrList = [];
var nameNum = 0; //点击左右滑动的下标
var nameNum1 = 0; //点击左右滑动的下标

var seter_timer;
// 获取首页栏目和内容接口
function loadContent() {
  $.server({}, '/website/v1/indexCategoryContent').then(function (res) {
    var result = res.result;
    result.forEach(function (element) {
      // 要闻栏目数据
      if (element.categoryId == 14) {
        let elHtml_1 = '';
        element.children.forEach(function (item, index) {
          if (item.categoryId == 15) {
            newsColumn_1 = item.contentList;

          }
          if (item.categoryId == 16) {
            newsColumn_2 = item.contentList;
          }
          if (item.categoryId == 17) {
            newsColumn_3 = item.contentList;
          }
          $("#news1nav>li").eq(index).text(item.categoryName)
        });
      }
      // "汇智保险智库"数据
      if (element.categoryId == 18) {
        element.children.forEach(function (item, index) {
          // "行业资料"数据
          if (item.categoryId == 19) {
            informationIndustry_1 = item.contentList;
          }
          // ""实践研究""数据
          if (item.categoryId == 23) {
            practice_1 = item.contentList;
          }
          // "合作交流"数据
          if (item.categoryId == 27) {
            cooperation_1 = item.contentList;
          }
          // "行业资料"数据
          if (item.categoryId == 37) {
            loadNewsArr(item.contentList, 7);
          }
          // ""实践研究""数据
          if (item.categoryId == 38) {
            loadNewsArr(item.contentList, 8);
          }
          // "合作交流"数据
          if (item.categoryId == 39) {
            loadNewsArr(item.contentList, 9);
          }
          $(".insurance").eq(index).text(item.categoryName);
        });
      }
      // "经济保险频道"数据
      if (element.categoryId == 31) {
        element.children.forEach(function (item, index) {
          if (item.categoryId == 32) {
            economics_1 = item.contentList;
          }
          if (item.categoryId == 33) {
            economics_2 = item.contentList;
          }
          if (item.categoryId == 34) {
            economics_3 = item.contentList;
          }
          $(".economy").eq(index).text(item.categoryName);
        });
      }
    });
    //调用server加载首页新闻
    loadNews(newsColumn_1,true);

    loadNewsArr(informationIndustry_1, 1);
    loadNewsArr(practice_1, 2);
    loadNewsArr(cooperation_1, 3);



    economics(economics_1, 4);
    economics(economics_2, 5);
    economics(economics_3, 6);

    //加载横向滚动的消息
    // loadMessage(newsColumn_1);

    // salon(economics_3, 6) //行业沙龙
  });
}
//加载横向滚动的消息
function loadMessage(obj) {
  $("#scroll_begin").html("");
  var html = '';
  console.log("消息列表", obj);
  for (var i = 0; i < obj.length; i++) {
    html += "<span onclick=\"goNewsDetail(" + obj[i].contentId + ",'\u9996\u9875','\u8981\u95FB\u680F\u76EE')\" class=\"slider_msg\" href=\"\">" + obj[i].title + "</span>";
  }
  $("#scroll_begin").html(html);
  // ScrollImgLeft();
}

function loadNews(arr,idx) {
  if (!arr) {
    $("#news1Content").html('');
    return false;
  }

  var obj = arr;
  var html = '',
      length = obj.length > 10 ? 10 : obj.length;

  for (var i = 0; i < length; i++) {
    if(idx){
      html += "<li onclick=\"goNewsDetail(" + obj[i].contentId + ",'\u9996\u9875','\u8981\u95FB\u680F\u76EE')\"> \n              <span class=\"newsTimer\">" + obj[i].createTime + "</span>  <span class=\"fenge\">|</span><a>" + obj[i].title + "</a>  \n            </li>";
    }else{
      html += "<li class=\"forli\" onclick=\"goNewsDetail(" + arr[i].contentId + ",'\u9996\u9875','\u8981\u95FB\u680F\u76EE')\"> <span class='squere'></span><a>" + arr[i].title + "</a>  <span>" + arr[i].createTime.substring(5, 10) + "</span></li>";
    }
  }
  $("#news1Content").html(html);
  if(idx){
    roll(50);
  }else{
    $("#news2Content").html("");
  }
  
}
// 专家列表数据
function expertArr(type) {
  type=type ? type : 2;
  $.server({ pageSize: 20, pageNum: 1 ,type : type}, '/website/v1/lecturerList').then(function (res) {
    expertArrList = res.result.data;
    if (expertArrList.length==0) {
      $('#expertRendering').html('');
      return
    }
    var name = expertArrList[0].name + "/" + expertArrList[0].title + " " + expertArrList[0].introduction;
    $('#exportDescription').html(name);
    expertRendering(expertArrList);
  });
}

//专家渲染
function expertRendering(arr) {
  if (!arr) {
    $('#expertRendering').html('');
    return false;
  }
  var html = '';
  for (var i = 0; i < arr.length; i++) {
    html += "<div class=\"swiper-slide\" > \n                    <a href=\"expertDetail.html?id=" + arr[i].lecturerId + "\"> \n                      <img src=\"" + arr[i].avatar + "\" alt=\"\" /> \n                      <div class=\"lecturerName\">" + arr[i].name + "</div>\n                      <div class=\"\">\u4E2D\u534F\u4FDD\u9996\u5E2D</div>\n                      <div class=\"\">\u4EBA\u5DE5\u667A\u80FD\u4E13\u5BB6</div>\n                    </a>\n                  </div>\n\n                ";
  }
  $('#expertRendering').html(html);
  var swidth = $("#swiper2").width();
  var slidesPerView = swidth / 300 > 3 ? 5 : 2;
  var swiper2 = new Swiper('#swiper2', {
    slidesPerView: slidesPerView,
    spaceBetween: 30,
    initialSlide: 2,
    centeredSlides: true,
    observer: true, //修改swiper自己或子元素时，自动初始化swiper
    observeParents: true, //修改swiper的父元素时，自动初始化swiper
    autoplayDisableOnInteraction: false, //解决手动滑动后不会自己轮播问题
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev'
    },
    on: {
      slideChangeTransitionEnd: function slideChangeTransitionEnd() {
        $('#exportDescription').html(arr[this.activeIndex].name + '/' + arr[this.activeIndex].title + arr[this.activeIndex].introduction);
      }
    }
  });
}
function lunbo(realTinfoArrList) {
  // 上面的大图轮播
  var swiper1 = new Swiper('#swiper1', {
    slidesPerView: 1,
    autoplay: true, //自动切换
    observer: true, //修改swiper自己或子元素时，自动初始化swiper
    observeParents: true, //修改swiper的父元素时，自动初始化swiper
    autoplayDisableOnInteraction: false, //解决手动滑动后不会自己轮播问题
    on: {
      slideChangeTransitionEnd: function slideChangeTransitionEnd() {
        $('#newsImageIns').html(realTinfoArrList[this.activeIndex].title);
        // console.log(this.activeIndex);//切换结束时，告诉我现在是第几个slide
      }
    }
  });
}
// 资讯列表数据
function realTinfo() {
  $.server({navigationType:1}, '/website/v1/hotContent').then(function (res) {
    realTinfoArrList = res.result;
    realTinfoRendering(realTinfoArrList);
    $('#newsImageIns').html(realTinfoArrList[0].title);
    lunbo(realTinfoArrList);
  });
}

//资讯渲染
function realTinfoRendering(arr) {
  if (!arr) {
    $('#realTinfoRendering').html('');
    return false;
  }
  var html = '';
  for (var i = 0; i < arr.length; i++) {
    html += " <div  class=\"swiper-slide swiper-slide-reset\" onclick=\"goNewsDetail(" + arr[i].contentId + ",'\u9996\u9875','\u8981\u95FB\u680F\u76EE')\"><img src=\"" + arr[i].poster + "\" alt=\"\"></div>";
    $('#realTinfoRendering').html(html);
  }
}



//加载行业新闻  实践资料  合作交流 统一html处理
function loadNewsArr(arr, index) {
  if (!arr) {
    $("#newsFox_" + index).html('');
    return false;
  }
  var html = '';
  var length = arr.length > 6 ? 6 : arr.length;
  for (var i = 0; i < length; i++) {
    if (i == 0) {
      html += " <li class=\"theFirstNews\" onclick=\"goNewsDetail(" + arr[i].contentId + ",'\u9996\u9875','\u6C47\u667A\u4FDD\u9669\u667A\u5E93')\">\n                          <div class='ani_imgBox'>  <img src=\"" + arr[i].poster + "\" alt=\"\">\n     </div>                       <div class=\"aquer\">\n                                <p class=\"firstTit\">" + arr[i].title + "</p>\n                                <p class=\"firstcont\">" + arr[i].description + "</p>\n                            </div>\n                        </li>";
    } else {
      html += "<li onclick=\"goNewsDetail(" + arr[i].contentId + ",'\u9996\u9875','\u6C47\u667A\u4FDD\u9669\u667A\u5E93')\"> <span class='squere'></span><a>" + arr[i].title + "</a>  <span>" + arr[i].createTime.substring(5, 10) + "</span></li>";
    }
  }
  $("#newsFox_" + index).html(html);
}

//经济保险
function economics(arr, index) {
  if (!arr) {
    $("#newsFox_" + index).html('');
    return false;
  }
  var html = '';
  var length = arr.length > 6 ? 6 : arr.length;
  for (var i = 0; i < length; i++) {
    if (i == 0) {
      html += " <li class=\"theFirstNews\" onclick=\"goNewsDetail(" + arr[i].contentId + ",'\u9996\u9875','\u7ECF\u6D4E\u4FDD\u9669\u9891\u9053')\">\n      <p class=\"firstTit\">" + arr[i].title + "</p>\n      <div class=\"aquer comflex\">\n         <div class='ani_imgBox'> <img src=\"" + arr[i].poster + "\" alt=\"\">\n </div>          <p class=\"firstcont\">" + arr[i].description + "</p>\n      </div>\n     </li>";
    } else {
      html += "<li onclick=\"goNewsDetail(" + arr[i].contentId + ",'\u9996\u9875','\u7ECF\u6D4E\u4FDD\u9669\u9891\u9053')\"> <span class='squere'></span><a>" + arr[i].title + "</a>  <span>" + arr[i].createTime.substring(5, 10) + "</span></li>";
    }
  }
  $("#newsFox_" + index).html(html);
}

function addTime(num) {
  num = parseInt(num);
  return num < 10 ? "0" + num : num;
}
//行业沙龙 已废弃
function salon(arr, index) {
  if (!arr) {
    $("#newsFox_" + index).html('');
    return false;
  }
  var html = '';
  var length = arr.length > 3 ? 3 : arr.length;
  for (var i = 0; i < length; i++) {
    var timeText = '07-08';
    try {
      var date = new Date(arr[i].createTime);
      timeText = addTime(date.getMonth() + 1) + "-" + addTime(date.getDate());
    } catch (err) {}
    html += " <li class=\"theFirstNews\" onclick=\"goNewsDetail(" + arr[i].contentId + ",'\u9996\u9875','\u7ECF\u6D4E\u4FDD\u9669\u9891\u9053')\">\n      <img src=\"" + arr[i].poster + "\" alt=\"\">\n      <div class=\"thiright\">\n          <div class=\"hyslTit\">" + arr[i].title + "</div>\n          <div class=\"themRow\"><span>\u4E3B\u9898:</span> " + arr[i].description + "</div>\n          <div class=\"last_row\">\n              <div style=\"width:40%\">" + arr[i].subTitle + "</div>\n              <div><span> \u65F6\u95F4:</span>" + timeText + "</div>\n          </div>\n      </div>\n  </li>";
  }
  $("#newsFox_" + index).html(html);
}

//加载课程 直播
function loadClass(index) {
  $.server({ courseCategoryId: index }, '/website/v1/hotCourse').then(function (res) {

    var obj = res.result;
    var html = '',
        length = obj.length > 8 ? 8 : obj.length;
    if (index == 1 || index == 2 || index == 3) {
      for (var i = 0; i < length; i++) {
        var priceHtml = "";
        if (obj[i].priceStr == 0) {
          priceHtml = "<span>免费</span>";
        } else {
          priceHtml = "<span>\uFFE5" + obj[i].priceStr + "</span>";
        }
        // html+=`<li> <span class='squere'></span><a>${obj[i].title}</a>  <span>${obj[i].time}</span></li>`
        html += "<div class=\"classFox\" onclick=\"goLessonDetail(" + obj[i].courseId + "," + index + ")\">\n              <div class='ani_imgBox'>      <img src=\"" + obj[i].poster + "\" alt=\"\">\n        </div>          <div class=\"className\">" + obj[i].title + "</div>\n                  <div class=\"classnum\">\u5171" + obj[i].catalogCount + "\u8282 </div>\n                  <div class=\"classpirce\">" + priceHtml + " " + obj[i].buyCount + "\u4EBA\u8D2D\u4E70</div>\n              </div>";
      }
    } else if (index == 4) {
      //直播
      for (var _i = 0; _i < length; _i++) {
        html += "<div class=\"lessonItem flexLay\" >\n                          <div class=\"lessonLeft\">\n                      <div class='ani_imgBox'>   <img src=\"" + obj[_i].poster + "\" alt=\"\">\n     </div>                          <div class = \"lessonInfo\" onclick = \"goLessonDetail(" + obj[_i].courseId + "," + index + ")\" >\n                                <div class=\"lessonTit\" data-id=\"" + obj[_i].courseId + "\">" + obj[_i].title + "</div>\n                                <div class=\"lessonTecher\">\n                                    <img src=\"" + obj[_i].lecturerAvatar + "\" alt=\"\">\n                                    <span>" + obj[_i].lecturerName + "</span>\n                                </div>\n                            </div>\n                        </div>\n                        <div class=\"lessonRight comflex\">\n                            <div class=\"timer\">" + obj[_i].liveTime + "</div>\n                            <div class=\"appointment\" id=\"appointment\" onclick=\"appointment(" + obj[_i].courseId + ")\">\u9884\u7EA6</div>\n                        </div>\n                    </div>";
      }
    }

    $("#classBox").html(html);
  });
}
/**
 * 跳转到直播详情
 */
function goLessonDetail(id, index) {
  location.href = "trainDetail.html?id=" + id + "&route=1&index=" + index;
}

// 页面顶部大图数据(待定)
function posterImg() {
  $.server({ location: 1 }, '/website/v1/getPoster').then(function (res) {
    $('#bannerImg').attr("src", res.result.data[0].url);
  });
}
/**
 * 页面初始化时加载 window.onload
 */
$(function () {
  posterImg();
  loadContent();

  //加载底部课程
  loadClass(1);
  realTinfo();
  expertArr();
  $(".hyzs").click(function () {
    location.href = "industry.html";
  });
  //加载行业新闻  实践资料  合作交流
  // loadSeconedNews()
  $("#news1nav>li").click(function (e) {
    var index = e.currentTarget.dataset.index;
    if ($(this).hasClass('active')) {
      return;
    }
    $("#news1nav li").removeAttr("class", "active");
    $(this).addClass("active");
    if (index == 1) {
      //行业要闻
      loadNews(newsColumn_1,true);
    } else if (index == 2) {
      //最新研究
      loadNews(newsColumn_2);
    } else if (index == 3) {
      //通知通告
      loadNews(newsColumn_3);
    }
  });
  // "汇智保险智库行业资料"
  // $("#hyzlBox>div").click(function (e) {
  //   $("#hyzlBox>div").removeClass("hyActive");
  //   $(this).addClass("hyActive");
  //   var type = e.currentTarget.dataset.type;
  //   $(".viewArticleList1").attr("href", "articleList.html?id=" + type);
  //   if (type == 20) {
  //     //政策法规
  //     loadNewsArr(informationIndustry_1, 1);
  //   } else if (type == 21) {
  //     //信息披露
  //     loadNewsArr(informationIndustry_2, 1);
  //   } else if (type == 22) {
  //     //保险知识
  //     loadNewsArr(informationIndustry_3, 1);
  //   }
  // });
  // // "汇智保险智库实践研究"
  // $("#hyzlBox1>div").click(function (e) {
  //   $("#hyzlBox1>div").removeClass("hyActive");
  //   $(this).addClass("hyActive");
  //   var type = e.currentTarget.dataset.type;
  //   $(".viewArticleList2").attr("href", "articleList.html?id=" + type);
  //   if (type == 24) {
  //     //专家观点
  //     loadNewsArr(practice_1, 2);
  //   } else if (type == 25) {
  //     //亚久报告
  //     loadNewsArr(practice_2, 2);
  //   } else if (type == 26) {
  //     //行业指数
  //     loadNewsArr(practice_3, 2);
  //   }
  // });
  // // "汇智保险智库合作交流"
  // $("#hyzlBox2>div").click(function (e) {
  //   $("#hyzlBox2>div").removeClass("hyActive");
  //   $(this).addClass("hyActive");
  //   var type = e.currentTarget.dataset.type;
  //   $(".viewArticleList3").attr("href", "articleList.html?id=" + type);
  //   if (type == 28) {
  //     //院校课题
  //     loadNewsArr(cooperation_1, 3);
  //   } else if (type == 29) {
  //     //研究成果
  //     loadNewsArr(cooperation_2, 3);
  //   } else if (type == 30) {
  //     //课题检索
  //     loadNewsArr(cooperation_3, 3);
  //   }
  // });

  //课程类型切换
  $("#classUl>li").click(function (e) {
    var index = e.currentTarget.dataset.index;
    if ($(this).hasClass('active')) {
      return;
    }
    $("#classUl li").removeAttr("class", "active");
    $(this).addClass("active");
    loadClass(index);
  });
  //专家轮播切换
  $(".hzshadTopText").click(function(e){
    $(".hzshadTopText").removeClass("activeText");
    $(this).addClass("activeText");
    var type=e.currentTarget.dataset.type;
    console.log(type);
    expertArr(type);
  })
  $(".bs_more").click(function(e){ 
    var id= $(this).attr('value');
    var text = $(this).prev().text();
    location.href="articleList.html?id="+id+"&title=首页&text="+text;
  })
});
//文字横向滚动
// function ScrollImgLeft() {
//   var speed = 50;
//   var MyMar = null;
//   var scroll_begin = document.getElementById("scroll_begin");
//   var scroll_end = document.getElementById("scroll_end");
//   var scroll_div = document.getElementById("scroll_div");
//   scroll_end.innerHTML = scroll_begin.innerHTML;
//   function Marquee() {
//     if (scroll_end.offsetWidth - scroll_div.scrollLeft <= 0) scroll_div.scrollLeft -= scroll_begin.offsetWidth;else scroll_div.scrollLeft++;
//   }
//   MyMar = setInterval(Marquee, speed);
//   scroll_div.onmouseover = function () {
//     clearInterval(MyMar);
//   };
//   scroll_div.onmouseout = function () {
//     MyMar = setInterval(Marquee, speed);
//   };
// }
function roll(t) {
  clearInterval(seter_timer);
  var ul1 = document.getElementById("news1Content");
  var ul2 = document.getElementById("news2Content");
  var ulbox = document.getElementById("jd_hotNewsBox");
  ul2.innerHTML = ul1.innerHTML;
  ulbox.scrollTop = 0; // 开始无滚动时设为0
  seter_timer = setInterval(rollStart, t); // 设置定时器，参数t用在这为间隔时间（单位毫秒），参数t越小，滚动速度越快
  // 鼠标移入div时暂停滚动
  ulbox.onmouseover = function () {
    clearInterval(seter_timer);
  }
  // 鼠标移出div后继续滚动
  ulbox.onmouseout = function () {
    seter_timer = setInterval(rollStart, t);
  }
}
// 开始滚动函数
function rollStart() {
  // 上面声明的DOM对象为局部对象需要再次声明
  var ul1 = document.getElementById("news1Content");
  var ul2 = document.getElementById("news2Content");
  var ulbox = document.getElementById("jd_hotNewsBox");
  // 正常滚动不断给scrollTop的值+1,当滚动高度大于列表内容高度时恢复为0
  if (ulbox.scrollTop >= ul1.scrollHeight) {
    ulbox.scrollTop = 0;
  } else {
    ulbox.scrollTop++;
  }
}